<?php

	/**

	* 

	*/

import('ORG.Util.JSSDK');

	class BookentAction extends Action

	{

		

		public function index()

		{

   		$jssdk = new JSSDK("wx9d8f3d6a105a7b28", "9638441d52c7b712637ead74d0922401");

        $signPackage = $jssdk->GetSignPackage();

        //dump($signPackage);

        $this->assign('signpackage',$signPackage);

			$this->display();

		}

		public function Book(){

			$isbm = explode(',', $_GET['isbm']);

			$isbm = $isbm[1];
			$url = 'https://api.douban.com/v2/book/search?q='.$isbm;
			$content = file_get_contents($url);
			$array = json_decode($content,true);
			$data = $array['books'][0];
			//dump($data);die;
			$this->assign('data',$data);
			$this->display();
		}
		public function add(){
			$data = I('post.','');
			//dump($data);die;
			$isbm = $data['book_isbm'];
			$fr = M('book')->where(array('book_isbm'=>$isbm))->find();
			if($fr){
				$this->error('该书已存在');
			}else{
			$res = M('book')->add($data);
			if($res){
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}
		}
		}
		public function lend(){
		$jssdk = new JSSDK("wx9d8f3d6a105a7b28", "9638441d52c7b712637ead74d0922401");
        $signPackage = $jssdk->GetSignPackage();
        //dump($signPackage);
        $this->assign('signpackage',$signPackage);
        $this->display();
		
		}
		   function findNum($str){
		        $str=trim($str);
		        if(empty($str)){return '';}
		        $result='';
		        for($i=0;$i<strlen($str);$i++){
		            if(is_numeric($str[$i])){
		                $result.=$str[$i];
		            }
		        }
		        return $result;
		    }
		public function dolend(){
			$info = $_GET['info'];
		 	$exinfo = explode(',',$info);
		 	if (substr($info,0,1)=='g') {       //还书扫描处理
		 		$exinfo = explode(',',$info);
		 		$bid = $this->findNum($exinfo[0]);
		 		$wid = $this->findNum($exinfo[1]);
		 		//dump($bid.'+'.$wid);
		 		$ret = M('lendlog')->where(array('wid'=>$wid,'bid'=>$bid))->find();
		 		if($ret){
		 			$bookinfo = M('book')->where(array('id'=>$bid))->find();
		 			$b['num'] = $bookinfo['num']+1;
		 			$ress = M('book')->where(array('id'=>$bid))->save($b);
		 			$a['status'] = 2;
		 			$a['backtime'] = time();
		 			$res =  M('lendlog')->where(array('wid'=>$wid,'bid'=>$bid))->save($a);
		 			if ($res&&$ress) {
							$this->success('还书成功');
						}else{
							$this->error('还书失败');
						}
		 		}else{
		 			$this->error('该同学未接此书');
		 		}
		 	}else{                               //借书扫描处理
		 		$array = array();
			foreach ($exinfo as $key => $value) {
				array_push($array,explode(':',$value));
			}
			foreach ($array as $key => $value) {
				if($value[0]=='wid'){
					$wid = $value[1];
				}
				if($value[0]=='bid'){
					$bid = $value[1];
				}
			}
			//dump('bid='.$bid.',wid='.$wid);
			$ret = M('lendlog')->where(array('wid'=>$wid,'bid'=>$bid))->find();
			if($ret['status']==1){
				$this->error('该同学已经借了此书');
			}else{
			$ret['status'] = 1;
			$ret['lendtime'] =time();
			$bookinfo = M('book')->where(array('id'=>$bid))->find();
			$a['num'] = $bookinfo['num']-1;
			$ress = M('book')->where(array('id'=>$bid))->save($a);
			$res = M('lendlog')->where(array('id'=>$ret['id']))->save($ret);
			if ($res&&$ress) {
				$this->success('借书成功');
			}else{
				$this->error('借书失败');
			}
		}
	}
		 }
}
?>